package com.sinder.campus_prophet.web.jwgl;

import cc.lyceum.api.thxy.jwgl.ThxyJwgl;
import com.sinder.campus_prophet.core.annotation.RequestLimit;
import com.sinder.campus_prophet.core.global.ResponseMessage;
import com.sinder.campus_prophet.service.jwgl.TbAchievementService;
import com.sinder.campus_prophet.service.jwgl.TbJwglInfoService;
import com.sinder.campus_prophet.service.jwgl.bean.dto.AchievementDto;
import com.sinder.campus_prophet.service.jwgl.bean.dto.AchievementSynDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;

/**
 * 成绩控制
 * @author sinder
 * Date 2023/3/25/21:55
 **/
@RestController
@RequestMapping("achievement")
@Slf4j
public class AchievementController {
    private final TbAchievementService achievementService;

    private final TbJwglInfoService jwglInfoService;

    public AchievementController(TbAchievementService achievementService, TbJwglInfoService jwglInfoService) {
        this.achievementService = achievementService;
        this.jwglInfoService = jwglInfoService;
    }

    /**
     * 查询用户个人学期参数
     * @param openId
     * @return
     */
    @GetMapping("getJwglXqParam")
    public ResponseMessage getJwglXqParam(@NotBlank(message = "用户标识不能为空") String openId) {
        return achievementService.getJwglXqParam(openId);
    }

    /**
     * 成绩查询
     * @param achievementDto
     * @return
     */
    @PostMapping("getAchievementList")
    public ResponseMessage getAchievementList(@Validated @RequestBody AchievementDto achievementDto) {
        return achievementService.getAchievementList(achievementDto);
    }

    /**
     * 成绩同步 单独同步教务系统数据
     * RequestLimit(count=1, time=1800000)：半小时内不能超过1次同步操作，避免重复点击，恶意请求
     * @param achievementSynDto
     * @return
     */
    @RequestLimit(count = 3, time = 600000)
    @PostMapping("synAchievementData")
    public ResponseMessage synAchievementData(@Validated @RequestBody AchievementSynDto achievementSynDto, HttpServletRequest request) {
        // 先校验账号密码是否正确
        ThxyJwgl jwgl = jwglInfoService.checkJwglLogin(achievementSynDto.getOpenId(), request);
        return achievementService.synAchievementData(achievementSynDto, jwgl);
    }
}
